﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

using OVS.Business;
using OVS.CoreDomain.Business;
using OVS.CoreDomain;

namespace OVS
{
    public partial class VoterRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoggedIn"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            else 
            {
                ILoggedin loggedIn = (ILoggedin)Session["LoggedIn"];
                if (!loggedIn.IsAdmin)
                {
                    Response.Write("You must be logged in as Admin to view this page!");
                    Response.End();
                }
            }

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Mode"] != null)
                {
                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;
                    string _voterId = Request.QueryString["voterId"].ToString();
                    PopulateVoter(_voterId);
                }
            }

        }

        protected void PopulateVoter(string voterId)
        {
            IBusinessVoter businessVoter = new BusinessVoter();
            IVoter voter = businessVoter.GetVoter(voterId);

            ((DropDownList)SelectWard.FindControl("ddlDistrict")).SelectedValue = voter.DistrictId;

            SelectWard.FillPanchayat(voter.DistrictId);

            ((DropDownList)SelectWard.FindControl("ddlPanchayat")).SelectedValue = voter.PanchayatId ;
            SelectWard.FillWards(voter.PanchayatId);

            ((DropDownList)SelectWard.FindControl("ddlWard")).SelectedValue = voter.WardId;
            txtAddress.Text = voter.Address;
            txtname.Text = voter.Name;
            txtDateOfBirth.Text = voter.DateOfBirth.ToShortDateString();
            txtVoterId.Text = voter.VoterId;
            txtFatherName.Text = voter.FatherName;
            txtEmailId.Text = voter.EmailId;

            imgPhoto.ImageUrl = Page.ResolveClientUrl(@"~/Photos/Voters/" + voter.VoterId + ".jpg");
             
           

        }
       
        protected void btnSave_Click(object sender, EventArgs e)
        {
            IVoter voter = new Voter();

            
            voter.WardId = ((DropDownList)SelectWard.FindControl("ddlWard")).SelectedValue;
            voter.DistrictId = ((DropDownList)SelectWard.FindControl("ddlDistrict")).SelectedValue;
            voter.PanchayatId = ((DropDownList)SelectWard.FindControl("ddlPanchayat")).SelectedValue;

            voter.Name = txtname.Text.Trim();
            voter.FatherName = txtFatherName.Text.Trim();
            voter.Address = txtAddress.Text.Trim();
            voter.DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text.Trim());
            voter.EmailId = txtEmailId.Text.Trim();
            voter.VoterId = txtVoterId.Text.Trim();
           

            IBusinessVoter businessVoter = new BusinessVoter();
            if ((ViewState["Mode"] != null) && (ViewState["Mode"].ToString() == "Edit"))
                businessVoter.UpdateVoter(voter);  
            else
                businessVoter.AddVoter(voter);  

            if (voterPhoto.HasFile)
            {
                //int len = voterPhoto.PostedFile.ContentLength;
                //byte[] pic = new byte[len];
                //voterPhoto.PostedFile.InputStream.Read(pic, 0, len);
                //voter.Photo = pic;

                //string _photosFolder = ConfigurationManager.AppSettings.Get("VoterPhotosFolder");
                string _photoName =  voter.VoterId + ".jpg";
                string _mapPath = Server.MapPath("~/Photos/Voters/" + _photoName);
                voterPhoto.PostedFile.SaveAs(_mapPath);
            }

            Reset();

            lblMsg.Text = "Voter saved successfuly";


        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        protected void Reset()
        {
            //ddlDistrict.SelectedIndex = 0;
            //ddlPanchayat.Items.Clear();
            //ddlWard.Items.Clear();
            txtname.Text = string.Empty;
            txtFatherName.Text = string.Empty;
            txtVoterId.Text = string.Empty;
            txtEmailId.Text = string.Empty;
            txtDateOfBirth.Text = string.Empty;
            //txtPhoto.Text = string.Empty;
            txtAddress.Text = string.Empty;

        }

        //protected void SaveFile(string targetFolder, bool overwriteMode)
        //{
        //    UploadedFile upfile = radUploadAttachment.UploadedFiles[0];
        //    radUploadAttachment.TargetPhysicalFolder = targetFolder;
        //    string fullFilePath = string.Format("{0}\\{1}", targetFolder, upfile.GetName());

        //    upfile.SaveAs(fullFilePath, !overwriteMode);
        //}

    }

}
